package com.hwpt.chesedemo.presentation.viewmodel

import androidx.compose.runtime.Composable
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.ViewModel
import com.hwpt.chesedemo.presentation.theme.strings

class HomeViewModel : ViewModel() {

    @Composable
    fun getMenuItems(): List<MenuItem> {
        val strings = strings()
        return listOf(
            MenuItem(
                icon = "⚔️",
                title = strings.battle,
                description = strings.battleDescription,
                route = "battle"
            ),
            MenuItem(
                icon = "📚",
                title = strings.teaching,
                description = strings.teachingDescription,
                route = "teaching"
            ),
            MenuItem(
                icon = "🔍",
                title = strings.review,
                description = strings.reviewDescription,
                route = "review"
            ),
            MenuItem(
                icon = "📊",
                title = strings.evaluation,
                description = strings.analysisResults,
                route = "evaluation"
            ),
            MenuItem(
                icon = "⚙️",
                title = strings.settings,
                description = strings.settingsDescription,
                route = "settings"
            )
        )
    }

    fun getLayoutParameters(screenWidth: Dp): LayoutParameters {
        val isSmallScreen = screenWidth < 600.dp
        val isTablet = screenWidth >= 600.dp && screenWidth < 840.dp
        val isLargeTablet = screenWidth >= 840.dp

        val horizontalPadding = when {
            isSmallScreen -> 24.dp
            isTablet -> 48.dp
            else -> 72.dp
        }

        val verticalPadding = when {
            isSmallScreen -> 32.dp
            isTablet -> 48.dp
            else -> 64.dp
        }

        val titleFontSize = when {
            isSmallScreen -> 28.sp
            isTablet -> 32.sp
            else -> 36.sp
        }

        val subtitleFontSize = when {
            isSmallScreen -> 14.sp
            isTablet -> 16.sp
            else -> 18.sp
        }

        val subtitleBottomPadding = when {
            isSmallScreen -> 40.dp
            isTablet -> 50.dp
            else -> 60.dp
        }

        return LayoutParameters(
            isSmallScreen = isSmallScreen,
            isTablet = isTablet,
            isLargeTablet = isLargeTablet,
            horizontalPadding = horizontalPadding,
            verticalPadding = verticalPadding,
            titleFontSize = titleFontSize,
            subtitleFontSize = subtitleFontSize,
            subtitleBottomPadding = subtitleBottomPadding
        )
    }

    fun getGridColumns(screenWidth: Dp): Int {
        return when {
            screenWidth < 600.dp -> 1 // 小屏幕：单列
            screenWidth < 840.dp -> 2 // 中等屏幕：2列
            else -> 3 // 大屏幕：3列（但我们需要显示5个，所以实际可能还是2列更好）
        }
    }



    fun getIconFontSize(screenWidth: Dp, isSingleColumn: Boolean): TextUnit {
        if (isSingleColumn) {
            return if (screenWidth < 600.dp) 36.sp else 40.sp
        }

        return when {
            screenWidth < 600.dp -> 36.sp
            screenWidth < 840.dp -> 42.sp
            else -> 48.sp
        }
    }

    fun getTitleFontSize(screenWidth: Dp, isSingleColumn: Boolean): TextUnit {
        if (isSingleColumn) {
            return if (screenWidth < 600.dp) 18.sp else 20.sp
        }

        return when {
            screenWidth < 600.dp -> 16.sp
            screenWidth < 840.dp -> 18.sp
            else -> 22.sp
        }
    }

    fun getDescriptionFontSize(screenWidth: Dp, isSingleColumn: Boolean): TextUnit {
        if (isSingleColumn) {
            return if (screenWidth < 600.dp) 12.sp else 14.sp
        }

        return when {
            screenWidth < 600.dp -> 12.sp
            screenWidth < 840.dp -> 13.sp
            else -> 14.sp
        }
    }

}

data class LayoutParameters(
    val isSmallScreen: Boolean,
    val isTablet: Boolean,
    val isLargeTablet: Boolean,
    val horizontalPadding: Dp,
    val verticalPadding: Dp,
    val titleFontSize: TextUnit,
    val subtitleFontSize: TextUnit,
    val subtitleBottomPadding: Dp
)

data class MenuItem(
    val icon: String,
    val title: String,
    val description: String,
    val route: String
)